package de.slothsoft.commons.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;

public final class SerializationUtil {

	public static <T extends Serializable> T load(File file) throws IOException, ClassNotFoundException {
		try (FileInputStream inputStream = new FileInputStream(file);
				ObjectInputStream objectInput = new ObjectInputStream(inputStream);) {
			return (T) objectInput.readObject();
		}
	}

	public static void storeAll(File file, Serializable... serializables) throws IOException {
		try (OutputStream stream = new FileOutputStream(file);
				ObjectOutputStream objectStream = new ObjectOutputStream(stream);) {

			for (Serializable serializable : serializables) {
				objectStream.writeObject(serializable);
			}
		}
	}

	private SerializationUtil() {
		// hide this class
	}
}
